package com.csoa.community.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.collect.community.R;

public class CommonDialog extends Dialog {
	private String[] content = new String[] {};
	private OnListItemClickListener onListItemClickListener;
	private OnRefreshListener mOnRefreshListener;
	private TextView tvTitle;
	private Button btnCancel;
	private Button dialogRefreshBtn;
	private ListViewAdapter listAdapter;

	public CommonDialog(Context context) {
		super(context);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.plug_dialog);
		ListView listView = (ListView) this
				.findViewById(R.id.caculator_listview);
		listAdapter = new ListViewAdapter(this.getContext());
		listView.setAdapter(listAdapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				dismiss();
				onListItemClickListener.onListItemClick(position);
			}
		});

		btnCancel = (Button) this.findViewById(R.id.btn_dialog_cancel);
		btnCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		tvTitle = (TextView) this.findViewById(R.id.dialogTitle);
		dialogRefreshBtn = (Button)this.findViewById(R.id.dialogRefreshBtn);
		dialogRefreshBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				mOnRefreshListener.onRefresh();
			}
		});
		

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		
	}

	public void setItems(String[] content,
			OnListItemClickListener onListItemClickListener) {
		this.content = content;
		this.onListItemClickListener = onListItemClickListener;
	}
	
	public void notifyDataChanged(String[] content) {
		this.content = content;
		if(listAdapter != null) {
			listAdapter.notifyDataSetChanged();
		}
	}

	public void setDialogTitle(String title) {
		if (tvTitle != null) {
			tvTitle.setText(title);
		}
	}

	public interface OnListItemClickListener {
		public void onListItemClick(int position);
	}

	public void setOnListItemClickListener(
			OnListItemClickListener onListItemClickListener) {
		this.onListItemClickListener = onListItemClickListener;
	}
	
	public interface OnRefreshListener {
		public void onRefresh();
	}

	public void setOnRefreshListener(
			OnRefreshListener onRefreshListener) {
		this.mOnRefreshListener = onRefreshListener;
	}

	// listView��adapter
	class ListViewAdapter extends BaseAdapter {
		Context mContext;
		LayoutInflater inflater;

		public ListViewAdapter(Context context) {
			this.mContext = context;
			inflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return content.length;
		}

		@Override
		public Object getItem(int position) {
			return content[position];
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			HoldView holder = null;
			if (convertView == null) {
				holder = new HoldView();
				convertView = inflater.inflate(R.layout.plug_dialog_list_item, null);
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.caculator_item_name);
				convertView.setTag(holder);
			} else {
				holder = (HoldView) convertView.getTag();
			}
			holder.tv_name.setText(content[position]);
			return convertView;
		}

		public class HoldView {
			TextView tv_name;
		}
	}

}
